package com.example.iotmanagerment26.mapper;

import com.example.iotmanagerment26.domain.User;
import com.example.iotmanagerment26.domain.vo.UserCard;
import com.example.iotmanagerment26.domain.vo.UserOprecordVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    @Select("select * from user where username=#{userName}")
    User selectUserByName(String userName);

    @Select("select * from user")
    List<User> selectAllUser();

    @Insert("insert into user values (null,#{username},#{password},#{power},#{cardId})")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    int insertUser(User user);

    @Delete("delete from user where id=#{id}")
    int deleteUserById(int id);

    @Update("update user set password=#{password} where username=#{name}")
    int updatePasswordByName(String name, String password);

    @Select("select * from user where username=#{username} and password=#{password}")
    User login(User user);

    @Select("select * from user where id=#{id}")
    @Results({
            @Result(
                    property = "cardId",
                    column = "cardId"
            ),
            @Result(column = "cardId",
                    property = "card",
                    one = @One(select = "com.example.iotmanagerment26.mapper.CardMapper.selectCardById"))
    })
    UserCard selectUserWithCardById(int id);


    @Select("select  * from user")
    @Results({
            @Result(
                    property = "cardId",
                    column = "cardId"
            ),
            @Result(column = "cardId",
                    property = "card",
                    one = @One(select = "com.example.iotmanagerment26.mapper.CardMapper.selectCardById"))
    })
    List<UserCard> selectAllUserWithCard();

    @Select("select * from user where id=#{id}")
    @Results({
            @Result(
                    property = "username",
                    column = "username"
            ),
            @Result(
                    property = "ops",
                    column = "username",
                    many = @Many(select = "com.example.iotmanagerment26.mapper.OprecordMapper.selectOprecordsByName")
            )
    })
    UserOprecordVO selectUserOprecordsById(int id);

    @Select("select * from user where power=#{power}")
    List<User> selectAllUserByPower(String power);
}
